﻿#include "GUIPropertyTypeDialog.h"
#include "ui_GUIPropertyTypeDialog.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossCase.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractSection.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include <QMessageBox>

namespace GUI
{
    GUIPropertyTypeDialog::GUIPropertyTypeDialog(QWidget* parent) : QDialog(parent)
        , _ui(new Ui::GUIPropertyTypeDialog)
    {
        _ui->setupUi(this);
        _ui->comboBox_type->addItem(tr("Solid"));
        _ui->comboBox_type->addItem(tr("Shell"));
        _ui->lineEdit->setReadOnly(true);
        _ui->lineEdit->setText("PROP/TYPE14");
        //获取材料管理器
        Radioss::FITKRadiossCase* caseObj = FITKAPP->getGlobalData()->
            getPhysicsData<Radioss::FITKRadiossCase>();
        if (caseObj == nullptr) return;
        auto matMgr = caseObj->getSectionManager();
        if (!matMgr)return;
        _ui->lineEditName->setText(matMgr->checkName("Property-1"));
    }

    GUIPropertyTypeDialog::~GUIPropertyTypeDialog()
    {
        if (_ui) delete _ui;
        _ui = nullptr;
    }

    bool GUIPropertyTypeDialog::isSolidType()
    {
        if(_ui->comboBox_type->currentText()=="Solid")
            return true;
        return false;
    }

    QString GUIPropertyTypeDialog::getName()
    {
        return _ui->lineEditName->text();
    }

    void GUIPropertyTypeDialog::on_pushButtonOK_clicked()
    {
        //获取RadiossCase
        Radioss::FITKRadiossCase* caseObj = FITKAPP->getGlobalData()->
            getPhysicsData<Radioss::FITKRadiossCase>();
        if (caseObj == nullptr) return;
        //获取材料管理器
        auto matMgr = caseObj->getSectionManager();
        if (!matMgr)return;
        //获取名称，查重处理
        QString name = _ui->lineEditName->text();
        if (name.isEmpty()) {
            QMessageBox::warning(this, "", tr("Name cannot be empty."), QMessageBox::Ok);
            return ;
        }
        else if (matMgr->getDataByName(name)) {
            QMessageBox::warning(this, "", tr("\"%1\" The name already exists").arg(name), QMessageBox::Ok);
            return ;
        }
        this->accept();
    }

    void GUIPropertyTypeDialog::on_pushButtonCancel_clicked()
    {
        this->reject();
    }

    void GUIPropertyTypeDialog::on_comboBox_type_currentIndexChanged(int index)
    {
        if (index == 0) 
            _ui->lineEdit->setText("PROP/TYPE14");
        else
            _ui->lineEdit->setText("PROP/TYPE1");
    }

}
